package gold.enterprise;

import java.util.HashSet;
import java.util.Set;

public class CoupangThree {

    //2. Find longest substring without repeating characters
//
//    Input: "abcabcbb" / Output: 3
//    Explanation: The answer is "abc", with the length of 3.
//
//    Input: "bbbbb" / Output: 1
//    Explanation: The answer is "b", with the length of 1.
//
//    Input: "pwwkew" / Output: 3
//    Explanation: The answer is "wke", with the length of 3.


    public int findLongestUniqueSubString(String s) {
        Set<Character> set = new HashSet<>();
        int left = 0, right = 0, maxLen = Integer.MIN_VALUE;
        while (right < s.length()) {
            char curChar = s.charAt(right);
            if (set.contains(curChar)) {
                //now we shall shrink the window, remove last element
                set.clear();
                left = right;
                set.add(s.charAt(left));
            } else {
                // increase the window
                maxLen = Math.max(maxLen, right - left + 1);
                set.add(s.charAt(right));
            }
            ++right;
        }

        return maxLen;
    }


    public static void main(String[] args) {
        CoupangThree solution = new CoupangThree();
        System.out.println(solution.findLongestUniqueSubString("pwwkew"));
    }


}
